<template>
	<view>
		<u-mask class="u-mask" :show="show">
			<view class="warp">
				<view class="title">
					发现新版本
				</view>
				<view class="content">
					<scroll-view scroll-y="true" class="scroll">
						<view class="content-title">
							{{updateData.version_name}}
							<!-- 版本的文字 -->
						</view>
						<view class="u-update-content">
							<rich-text :nodes="updateData.desc"></rich-text>
							<!-- 隔行的最好加上<br> -->
						</view>
					</scroll-view>
				</view>
				<view class="btn">
					<u-button @click="closeModal" v-if="!updateData.must_update" class="custom-style" :hair-line="false" hover-class="none" type="default">取消</u-button>
					<u-button @click="confirmWgt" class="custom-style comfirm" :hair-line="false" type="default" ripple-bg-color="rgba(255, 255, 255, 0.15)" :ripple="true">升级</u-button>
				</view>
			</view>
		</u-mask>
		<u-popup border-radius="30" v-model="isShowLoading" mode="center">
			<view class="loding">
				<image src="../../static/28x28.png" mode=""></image>
				<!-- uview组件 -->
				<u-line-progress active-color="#EB344A" :striped-active="true" :striped="true" :percent="percentSize"></u-line-progress>
				
				<!-- uniapp组件 -->
				<!-- <progress activeColor="#EB344A" :percent="percentSize"></progress> -->
				<text>正在下载安装中</text>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				content: `
					1. 修复badge组件的size参数无效问题<br>
					2. 新增Modal模态框组件<br>
					3. 新增压窗屏组件，可以在APP上以弹窗的形式遮盖导航栏和底部tabbar<br>
					4. 修复键盘组件在微信小程序上遮罩无效的问题
				`,
				isShowLoading:false,
				percentSize:0,
			}
		},
		props:{
			updateData:{
				type:Object,
				default:{},
				// updateData:{
				// 	href:"",这是下载的url
				// 	desc:"",描述，隔行的最好加上<br> desc.replace(/\r\n/g,'<br>')
				// 	version_name:""版本
				// }
			}
		},
		onLoad(e) {

		},
		onShow() {
			
		},
		methods: {
			maskShow(){
				this.show = true;
			},
			cancel() {
				this.closeModal();
			},
			// 资源更新
			confirmWgt() {
				this.isShowLoading = true;
				this.closeModal();
				this.downloadWgt();
				// this.closeModal();
			},
			// 整包更新
			confimApk(){
				this.downloadApk()
			},
			closeModal() {
				this.show = false;
			},
			// 整包更新apk,直接打开url
			downloadApk(){
				plus.runtime.openURL(this.updateData.href)
			},
			// 下载资源热更新
			downloadWgt(){
				// plus.nativeUI.showWaiting("下载wgt文件...");  
				let dark = plus.downloader.createDownload(this.updateData.href,{filename:"_doc/update/"},(d,status) =>{
				    if ( status == 200 ) {   
						console.log("下载wgt成功：" + JSON.stringify(d.filename));
						// plus.nativeUI.alert("下载wgt成功1");
						if(d.filename){
							this.installWgt(d.filename); // 安装wgt包
						}
				    } else {  
				        // console.log("下载wgt失败！");  
				        plus.nativeUI.alert("下载wgt失败！");  
				    }  
				    // plus.nativeUI.closeWaiting();  
				});

				dark.start();
				dark.addEventListener("statechanged",(download,status) =>{
					if(download.downloadedSize > 0){
						 this.percentSize = parseInt(download.downloadedSize/download.totalSize * 100);
						 console.log(this.percentSize,"百分比")
					}
					if(download.state == 4){
						this.isShowLoading = false;
						this.$emit("finshUpdate",true)
					}
				},false);
			},
			// 安装更新包
			installWgt(path){
				// plus.nativeUI.showWaiting("安装2"+path);  
				plus.runtime.install(path,{},function(){  
				    plus.nativeUI.closeWaiting();  
					// plus.runtime.restart();  
				    plus.nativeUI.alert("应用更新完成！",function(){  
				        plus.runtime.restart();  
				    }); 
					 
				},function(e){  
				    plus.nativeUI.closeWaiting();  
				    console.log("安装wgt文件失败["+e.code+"]："+e.message);  
				    plus.nativeUI.alert("安装wgt文件失败["+e.code+"]："+e.message);  
				});  
			},
		}
	}
</script>

<style scoped lang="scss">
	// @import "../../../../libs/css/style.components.scss";
	.loding{
		width: 500rpx;
		height: 400rpx;
		border-radius: 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-around;
		padding: 0 30rpx;
		image{
			width: 120rpx;
			height: 120rpx;
		}
		text{
			color: #666666;
		}
	}
	.u-full-content {
		background-color: #00C777;
	}
	
	.u-update-content {
		font-size: 26rpx;
		// color: $u-content-color;
		color: #FFFF00;
		line-height: 1.7;
		padding: 30rpx;
	}
	.u-mask{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.warp{
		background: url("../../static/28x28.png") center center no-repeat;
		background-size: cover;
		width: 655.2rpx;
		height: 714rpx;
		.title{
			display: flex;
			justify-content: center;
			color: #FFFFFF;
			padding: 60rpx 0;
			font-size: 42rpx;
			font-weight: bold;
		}
		.content{
			padding-top: 60rpx;
			.scroll{
				height: 340rpx;
			}
			.content-title{
				padding: 0 16px 0 16px;
			}
		}
		.btn{
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			.custom-style{
				border: none;
				margin: 0;
				width: 35%;
				background-color: #ccc;
			}
			.comfirm{
				background: linear-gradient(-90deg, rgba(235,52,74,.8), rgba(244,92,68,.8));
				color: #FFFFFF;
			}
		}
	}
</style>
